import { api } from "src/boot/axios";

export async function syncUserinfo(context) {
  const res = await api.get("/user/center");
  if (res) {
    context.commit("setUserinfo", res);
    context.commit("setAccounts", res.accounts_list);
  }
  return res;
}

export async function syncAccounts(context) {
  const res = await api.get("/accounts/index");
  if (res) {
    context.commit("setAccounts", res);
  }
  return res.data;
}

export async function modifyAccount(context, account) {
  await api.post("accounts/edit", account);
  syncAccounts(context);
}

export function logoutAccount(context, { accounts_id, userid }) {
  if (!accounts_id) {
    const acc = Object.values(context.state.accounts).find(
      (v) => v.userid == userid
    );
    accounts_id = acc.accounts_id;
  }
  api
    .post("accounts/logout", { accounts_id })
    .then(() => syncAccounts(context));
}
